import { range, count, of, max, min, reduce } from "rxjs";
export const countDemo = () => {
  const numbers = range(1, 7); // 1,2,3,4,5,6,7
  const result = numbers.pipe(count((i) => i % 2 === 1));
  result.subscribe((x) => console.log(x));
  // Results in: 偶数有 4 个
  // 4
};

export const maxDemo = () => {
  of({ age: 7, name: "Foo" }, { age: 5, name: "Bar" }, { age: 9, name: "Beer" })
    .pipe(
      max((a, b) => {
        //
        console.log(a);
        console.log(b);
        return a.age < b.age ? -1 : 1;
      }),
    )
    .subscribe((x) => console.log(x.name));

  // Outputs
  // 'Beer'
};

export const minDemo = () => {
  of({ age: 7, name: "Foo" }, { age: 5, name: "Bar" }, { age: 9, name: "Beer" })
    .pipe(
      min((a, b) => {
        //
        console.log(a);
        console.log(b);
        return a.age < b.age ? -1 : 1;
      }),
    )
    .subscribe((x) => console.log(x.name));
};

export const reduceDemo = () => {
  of(1, 2, 3)
    .pipe(
      reduce((pre, now) => {
        return pre + now;
      }),
    )
    .subscribe((x) => console.log(x)); // 6 类似js数组的 reduce 函数；
};
